package pl.edu.agh.inpema.components.receivers.phpBB2Receiver;

import java.util.LinkedList;
import java.util.List;

import org.htmlparser.util.ParserException;

import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.connector.ConnectionException;
import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.connector.ForumPageReceiver;
import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.model.Post;
import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.model.Subject;
import pl.edu.agh.inpema.components.receivers.phpBB2Receiver.model.Topic;

public class ForumAnalyzer {

	private ForumPageReceiver receiver;
	
	public ForumAnalyzer(String url, String login, String password, String postDetailsPattern, boolean asHtml, long sleepTime) throws ConnectionException {
		this.receiver = new ForumPageReceiver(url, login, password, postDetailsPattern, asHtml, sleepTime);
	}
	
	public List<Subject> getAllSubjects() throws ConnectionException, ParserException {
		return this.receiver.getAllSubjects();
	}
	
	public List<Topic> getAllTopicsFromSubjectAddress(String subjectAddress) throws ConnectionException, ParserException {
		List<Topic> topics = this.receiver.getAllTopicsFromSubjectAddress(subjectAddress);
		if (topics.size() > 0) {
			return topics;
		} else {
			List<Subject> subjects = this.receiver.getAllSubjects(subjectAddress);
			topics = new LinkedList<Topic>();
			for (Subject subject : subjects) {
				topics.addAll(this.getAllTopicsFromSubjectAddress(subject.getAddress()));
			}
			return topics;
		}
		//return this.receiver.getAllTopicsFromSubjectAddress(subjectAddress);
	}
	
	public List<Post> getAllPostsFromTopicAddress(String topicAddress) throws ConnectionException, ParserException {
		return this.receiver.getAllPostsFromTopicAddress(topicAddress);
	}
	
}
